
import 'package:flutter/material.dart';
import 'package:reabampos/src/reabam/operation/bean/socket/Request_server_ip_list.dart';
import 'package:reabampos/src/reabam/operation/bean/socket/Request_update_serversocket.dart';
import 'package:reabampos/src/reabam/operation/rb_api.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/operation/rb_enum.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_style.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
多端打印,绑定serverSocketPrinter
 */
class SocketServerIPListStatePage extends XPageListViewState{

  SocketServerIPListStatePage({
    super.isEnableManualAddOrRemoveItemAnimation=true
  });

  @override
  Widget getListItemWidget(listItem, int removeUsePosition) {
    String ip=listItem as String;

    return XWidgetUtils.getXBarForLeftRightWidget(
      margin: XWidgetUtils.getEdgeInsets(XTempData.defaultPageMarginLeft, 10, XTempData.defaultPageMarginRight, 0),
      isShowBottomDivider: false,
      leftWidget: XWidgetUtils.getLayoutExpanded(
        XWidgetUtils.getWidgetText(
            ip,
            style: XStyleUtils.getTextStyle(
                fontSize: XTempData.defaultTextFontSize,
                color: XColorUtils.color333333
            )
        ),
      ),
      rightWidget: XWidgetUtils.getWidgetText(
          '绑定',
          style: RBStyles.textStyle_reabamColor4_defaultSize()
      ),
      onPressed: (){
        finishPage(result: ip);
      },
    );

  }

  @override
  void onStart() {

    if(RBConfigs.versionType==VersionType.officialSelfServiceVersion
        || RBConfigs.versionType==VersionType.testSelfServiceVersion
    ){
      //自助收银
      setXTopBarWidget(
          height: null,
          child: RBWidgets_SelfService.getTitleBarWidget(
              this,
              title: '绑定主设备',
              rightBt: '刷新',
              onTapRightBt: (){
                updateData();
              }
          )
      );

    }else{
      //HD h5
      setXTitle(titleText: '绑定主设备',
        titleBarHeight: 50,
        isShowLeftWidget: false,
        rightWidgets:[
          XWidgetUtils.getWidgetButtonText(
              text: '刷新',
              buttonStyle: XStyleUtils.getButtonStyle(fontSize: 15,fontColor: RBColors.reabamColor4),
              onPressed: (){
                updateData();
              }
          )
        ],
      );
      //
      setXLeftFixSpace(width:458);
    }

  }


  @override
  Future<void> updateData({int pageIndex=1}) async {
    showLoading();
    RBBaseApi.getServerIPList(
        succeed: (ResponseGetServerIPList response, Map<String, dynamic> responseMap, Map<String, dynamic> headersResponse) {
          hideLoading();
          XLogUtils.printLog("----getServerIPList,succeed");
          List<dynamic> ipList=[];
          BeanGetSocketListData? data=response.data;
          if (data!=null){
            List<BeanSocketPrintSetDTOS>? printSetDTOS=data.printSetDTOS;
            if (printSetDTOS!=null){
              for (BeanSocketPrintSetDTOS item in printSetDTOS){
                if (item.isMain==1){
                  ipList.add(item.ip!);
                }
              }
            }
          }
          setXListData_addAllItems(ipList);

        },
        failed: (String code, String msg) {
          hideLoading();
          toast(msg);
        }

    );



  }

}